Skip to main content

Ternary Expressions

Ternary expressions behave identical as to how they would in C. They introduce no new keywords.

Old Code
pluto
local max
if a > b then
max = a
else
max = b
end
New Code
pluto
local max = a > b ? a : b

Try It Yourself

If Expressions

If expressions are an alternative syntax for ternary expressions:

pluto
local a = 6
local b = 9
local max = if a > b then a else b end
print(max) --> 9

Doesn't Lua already have ternaries?

While it is true that you can do something like this:

pluto
local max = a > b and a or b

Keep in mind that this falls apart when the true-expression has a falsy value:

pluto
local x = -1
x = (x == -1 and nil or x)

In this case, x will be -1 despite the intention being to set it to nil. There are no such issues using Pluto's ternary expressions.